package com.wy.office.swift.ppt.handler;

import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
import org.apache.poi.xwpf.usermodel.PositionInParagraph;
import org.apache.poi.xwpf.usermodel.TextSegment;
import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlString;
import org.openxmlformats.schemas.drawingml.x2006.main.CTRegularTextRun;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextCharacterProperties;

import com.wy.office.swift.AOfficeHandler;
import com.wy.office.swift.ppt.IPPTHandler;

/**
 * ppt处理器抽象类
 * @author wangyuan
 * @since 2019年4月8日 下午11:38:08
 *
 */
public abstract class APPTHandler extends AOfficeHandler implements IPPTHandler {

	/**
	 * 搜索
	 * @param paragraph
	 * @param searched
	 * @param startPos
	 * @return
	 */
	public TextSegment searchText(XSLFTextParagraph paragraph, String searched, PositionInParagraph startPos) {
        int startRun = startPos.getRun(),
            startText = startPos.getText(),
            startChar = startPos.getChar();
        int beginRunPos = 0, candCharPos = 0;
        boolean newList = false;
        
        CTRegularTextRun[] rArray = paragraph.getXmlObject().getRArray();
        for (int runPos = startRun; runPos < rArray.length; runPos++) {
            int beginTextPos = 0, beginCharPos = 0, textPos = 0, charPos = 0;
            CTRegularTextRun ctRun = rArray[runPos];
            XmlCursor c = ctRun.newCursor();
            c.selectPath("./*");
            try {
                while (c.toNextSelection()) {
                    XmlObject o = c.getObject();
                    if (o instanceof XmlString) {
                        if (textPos >= startText) {
                            String candidate = ((XmlString) o).getStringValue();
                            if (runPos == startRun) {
                                charPos = startChar;
                            } else {
                                charPos = 0;
                            }
    
                            for (; charPos < candidate.length(); charPos++) {
                                if ((candidate.charAt(charPos) == searched.charAt(0)) && (candCharPos == 0)) {
                                    beginTextPos = textPos;
                                    beginCharPos = charPos;
                                    beginRunPos = runPos;
                                    newList = true;
                                }
                                if (candidate.charAt(charPos) == searched.charAt(candCharPos)) {
                                    if (candCharPos + 1 < searched.length()) {
                                        candCharPos++;
                                    } else if (newList) {
                                        TextSegment segement = new TextSegment();
                                        segement.setBeginRun(beginRunPos);
                                        segement.setBeginText(beginTextPos);
                                        segement.setBeginChar(beginCharPos);
                                        segement.setEndRun(runPos);
                                        segement.setEndText(textPos);
                                        segement.setEndChar(charPos);
                                        return segement;
                                    }
                                } else {
                                    candCharPos = 0;
                                }
                            }
                        }
                        textPos++;
                    } else if (o instanceof CTTextCharacterProperties) {
                        //do nothing
                    } else {
                        candCharPos = 0;
                    }
                }
            } finally {
                c.dispose();
            }
        }
        return null;
    }
}
